<!DOCTYPE html>

<html lang="zh-Hans">
  <head>
    <meta charset="utf-8" />
    <meta name="author" content="rogepi" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <script src="/vue.global.js"></script>
    <title>2-computed-setter</title>
  </head>

  <body>
    <div id="computed-setter">
      <p>Your Name:</p>
      <span>{{fullName = 'zzz kkk'}}</span>
      <p>fitstName:</p>
      <span>{{firstName}}</span>
      <p>lastName:</p>
      <span>{{lastName}}</span>
    </div>
    <script>
      const vm = Vue.createApp({
        data() {
          return {
            firstName: 'Toky',
            lastName: 'Rogepi',
          };
        },
        // 计算属性默认只有 getter，不过在需要时你也可以提供一个 setter：
        computed: {
          fullName: {
            // getter
            get() {
              return this.firstName + ' ' + this.lastName;
            },
            // setter
            set(newValue) {
              const names = newValue.split(' ');
              this.firstName = names[0];
              this.lastName = names[names.length - 1];
            },
          },
        },
      }).mount('#computed-setter');
    </script>
  </body>
</html>
